/**
 * Created by jiexin on 2017/12/13.
 */
layui.use(['laypage', 'layer', 'table', 'form', 'element'], function() {
    var laypage = layui.laypage;//分页
    var layer = layui.layer; //弹层
    var table = layui.table; //表格
    var $ = layui.jquery;
    var form = layui.form;
    var element = layui.element; //元素操作


    var m_a = document.getElementById("divtwo");
    var m_a_h = m_a.offsetHeight -300;

    if (rule_level !==5){
        var $ubj = $("#username");
        var $rbj = $("#rule_id");
        for (var i =0; i<poor_list.length; i++){
            $ubj.append('<option value="'+ i + '" class="add-select">' + poor_list[i] + '</option>')
        }
        $.each(rule_json, function (a,b) {
            $rbj.append('<option value="'+ a + '" class="add-select">' + b + '</option>')
        });
        form.render('select')
    }

    form.on("submit(project_u)", function (udata) {
        var r_u_field = udata.field;
        r_u_field["username"] = $("#username").find("option:selected").text();
        var r_u_data = {"data":JSON.stringify(r_u_field), "type":0};
        majax(r_u_data);
        $("#initiate_u").trigger("click")
    });

    form.on("submit(project_b)", function (idata) {
        var r_i_field = JSON.stringify(idata.field);
        var r_i_data = {"data":r_i_field, "type":1};
        majax(r_i_data);
        $("#initiate_b").trigger("click")
    });


    function majax(mdata) {
        $.ajax({
            url:'/audit-manage-add/',
            dataType:'json',
            type:'POST',
            data:mdata,
            success: function (m_r_data) {
                if (m_r_data["status"] === 0){
                    var $umj = $("#username");
                    var $rmj = $("#rule_id");
                    $umj.find("option.add-select").remove();
                    $rmj.find("option.add-select").remove();
                    r_user_list = m_r_data["r_user_list"];
                    poor_list = m_r_data["poor_list"];
                    rule_json = m_r_data["rule_json"];
                    for (var i =0; i<poor_list.length; i++){
                        $ubj.append('<option value="'+ i + '" class="add-select">' + poor_list[i] + '</option>')
                    }
                    $.each(rule_json, function (a,b) {
                        $rbj.append('<option value="'+ a + '" class="add-select">' + b + '</option>')
                    });
                    form.render('select');
                    usetable.reload({
                        data:r_user_list
                    })
                } else if (m_r_data["status"] === 1) {
                    item_list = m_r_data["item_list"];
                    jenkinstable.reload({
                        data:item_list
                    })
                } else {
                    layer.msg("发生错误！")
                }
            }
        })
    }


    function modtool(mtype, mdata) {
        console.log(mtype);
        console.log(mdata);
        if (mtype === 0){
            $("#username_do").addClass("layui-hide");
            $("#username_dt").removeClass("layui-hide");
            $("#username_t").val(mdata["username"]);
            $("#username").removeAttr("lay-verify", "required");
            $("#project_u").addClass("layui-hide");
            $("#project_um").removeClass("layui-hide");
            $("#project_ux").removeClass("layui-hide");
        }else if(mtype === 1){
            $("#item_name").val(mdata["item_name"]);
            $("#item_jenkins").val(mdata["item_jenkins"]);
            $("#project_b").addClass("layui-hide");
            $("#project_bm").removeClass("layui-hide");
            $("#project_bx").removeClass("layui-hide");
        }
    }


    $("#project_ux").on("click", function () {
        $("#username").attr("lay-verify", "required");
        $("#username_do").removeClass("layui-hide");
        $("#username_dt").addClass("layui-hide");
        $("#project_u").removeClass("layui-hide");
        $("#project_um").addClass("layui-hide");
        $("#project_ux").addClass("layui-hide");

    });

    $("#project_bx").on("click", function () {
        $("#project_b").removeClass("layui-hide");
        $("#project_bm").addClass("layui-hide");
        $("#project_bx").addClass("layui-hide");
    });

    form.on("submit(project_um)", function (umdata) {
        var p_umdata = {"id":userid, "data":JSON.stringify(umdata.field),"type":0,"status":0};
        ops_dm(p_umdata);
        console.log(p_umdata)
    });

    form.on("submit(project_bm)", function (bmdata) {
        var p_bmdata = {"id":itemid, "data":JSON.stringify(bmdata.field),"type":0,"status":1};
        ops_dm(p_bmdata);
        console.log(p_bmdata)
    });

    function ops_dm(dm_data) {
        $.ajax({
            url:"/audit-ops/",
            dataType:"json",
            type:"POST",
            data:dm_data,
            success: function (ops_data) {
                if (dm_data["status"]===0){
                    usetable.reload({
                        data:ops_data["r_user_list"]
                    });
                    if (dm_data["type"]===0){
                        $("#project_ux").trigger("click")
                    }
                }else if(dm_data["status"]===1){
                    jenkinstable.reload({
                        data:ops_data["item_list"]
                    });
                    if (dm_data["type"]===0){
                        $("#project_bx").trigger("click")
                    }
                }
            }
        })
    }

    var table_format_use = [[
         {field: 'id', align: 'center', title: '编号', width: '15%'}
         , {field: 'username', align: 'center', title: '模块列表', width: '20%'}
         , {field: 'rule_name', align: 'center', title: '当前角色', width: '20%'}
         , {field: 'createDate', align: 'center', title: '发起时间', width: '25%'}
         , {fixed: 'right',width: '25%', align: 'center',  title: '操作', toolbar: '#uDemo', templet: '#uDemo'}
    ]];

    $('#audit_user').each(function() {
        //获取标签对于的一个属性
        var $obj = $(this);
        var tableid = $obj[0].id;
        var etaleid = '#' + tableid;
        var toolid = 'tool(' + tableid + ')';
        createtableu(etaleid,tableid,r_user_list,table_format_use,m_a_h);
        usetool(toolid,tableid);
    });

    var usetable;
    function createtableu(etaleid,tableid,datas,table_format,theight) {
        usetable = table.render({
            elem: etaleid,
            id: tableid,
            height:theight,
            limit: 10,
            page: true,
            data: datas,
            cols: table_format
        });
    }

    var userid;
    function usetool(toolid, tableid) {
        //监听工具条
        //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        table.on(toolid, function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            userid = data["id"];
            if (layEvent === "mod_u"){
                modtool(0,data)
            }else if(layEvent === "del_u"){
                var p_umdata = {"id":userid, "data":"","type":1,"status":0};
                ops_dm(p_umdata)
            }
        })
    }

    var table_format_jenkins = [[
         {field: 'id', align: 'center', title: '编号', width: '15%'}
         , {field: 'item_name', align: 'center', title: '项目名称', width: '20%'}
         , {field: 'item_jenkins', align: 'center', title: 'jenins配置名', width: '20%'}
         , {field: 'createDate', align: 'center', title: '发起时间', width: '25%'}
         , {fixed: 'right',width: '25%', align: 'center',  title: '操作', toolbar: '#jDemo', templet: '#jDemo'}
    ]];

    $('#audit_jenkins').each(function() {
        //获取标签对于的一个属性
        var $obj = $(this);
        var tableid = $obj[0].id;
        var etaleid = '#' + tableid;
        var toolid = 'tool(' + tableid + ')';
        createtablej(etaleid,tableid,item_list,table_format_jenkins, m_a_h);
        jenkinstool(toolid,tableid);
    });

    var jenkinstable;
    function createtablej(etaleid,tableid,datas,table_format, theight) {
        jenkinstable = table.render({
            elem: etaleid,
            id: tableid,
            height:theight,
            limit: 10,
            page: true,
            data: datas,
            cols: table_format
        });
    }

    var itemid;
    function jenkinstool(toolid, tableid) {
        //监听工具条
        //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        table.on(toolid, function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            itemid = data["id"];
            if (layEvent === "mod_j"){
                modtool(1,data)
            }else if(layEvent === "del_j"){
                var p_bmdata = {"id":itemid, "data":"","type":1,"status":1};
                ops_dm(p_bmdata)
            }
        })
    }



    $('.demoTable .layui-btn').on('click',
        function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        }
    );
});